<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once_from_inc('modules/admin_panel/forms/menu_item/menu_item_form.class.php');
require_once_from_inc('modules/admin_panel/forms/menu_item/menu_item_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tree_menu_ml/tree_menu_item_ml_dbc.class.php');


class Menu_Item_Form_Ctrl extends GForm_Ctrl_With_Object {
	private $prefix;
	private $parent;

	function __construct($system_name, Vars_Manager $vm, DB_Connection $dbc, $object_id_capture = 'obj_id', $prefix, $parent) {
		$this->prefix = $prefix;
		$this->parent = $parent;
		parent::__construct($system_name, $vm, $dbc, $object_id_capture);
	}


	protected function create_form() {
		$form = new Menu_Item_Form();

//		$form->generate_html();

		return $form;
	}


	protected function create_form_view() {
		$ret = new Menu_Item_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		$prefix = $this->get_permanent_external_param('prefix');
		if (!ereg("^[a-z]{1}[a-z0-9_]{0,100}$" , $this->form->get_field_value('hid'))) {
			$this->form->set_field_error('hid', 'invalid_hid');
			$ret = false;
		} else {
			$dbc = $this->get_dbc();
			$object = $this->get_object();
			if ($object->get_id()) {
				if (!Tree_Menu_Item_DBC::is_unique_hid_update($dbc, $this->form->get_field_value('hid'), $object->get_id(), $prefix.'menu')) {
					$this->form->set_field_error('hid', 'not_unique');
					$ret = false;
				}
			} else {
				if (!Tree_Menu_Item_DBC::is_unique_hid($dbc, $this->form->get_field_value('hid'), $prefix.'menu')) {
					$this->form->set_field_error('hid', 'not_unique');
					$ret = false;
				}
			}
		}

		return $ret;
	}


	protected function on_bad_submit() {

//		printbr('lajna');
	}


	protected function create_object() {
		$prefix = '';
		if ($this->prefix || $this->get_permanent_external_param('prefix', true)) {
			if ($this->prefix !== false) {
				$prefix = $this->prefix;
			} else {
				$prefix = $this->get_permanent_external_param('prefix');
			}
		}

		$object = new Tree_Menu_Item_ML_DBC($prefix.'menu', 1);

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('label', $object->get_label());
		if ($this->parent !== false) {
			$form->set_field_value('parent', $this->parent);
		} else {
			$form->set_field_value('parent', $object->get_parent());
		}
		$form->set_field_value('link', $object->get_link());
		$form->set_field_value('hid', $object->get_hid());
		$form->set_field_value('target', $object->get_target());
		$form->set_field_value('order', $object->get_order());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_label($form->get_field_value('label'));
		$object->set_parent($form->get_field_value('parent'));
		$object->set_link($form->get_field_value('link'));
		$object->set_hid($form->get_field_value('hid'));
		$object->set_target($form->get_field_value('target'));
		if ($form->get_field_value('order')) {
			$object->set_order($form->get_field_value('order'));
		} else {
			$object->set_order(10);
		}

//		if (!$object->get_newsletter()) {
//			$object->set_newsletter($this->get_permanent_external_param('newsletter_id'));
//		}
	}


	public function prepare_order($parent) {
		$dbc = $this->get_dbc();

		$next_ordering = Tree_Menu_Item_ML_DBC::get_next_order($dbc, $parent, $this->prefix);

		$form = $this->get_form();
		$object = $this->get_object();
		$object->set_order($next_ordering);
	}
}